package cn.operation.api.rest;


import cn.operation.api.dto.PriceSaleOlTpDTO;
import cn.operation.backend.service.PriceSaleOlTpService;
import cn.operation.backend.util.BusinessException;
import cn.operation.backend.util.ResultDTOWithPagination;
import cn.operation.base.model.LinePriceSaleOlTpTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/7 0007.
 */
@Controller
@RequestMapping(value = "/api", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(value = "/price", description = "产品标准售价")
public class PriceSaleOlTpRest {

	private static final Logger LOGGER = LoggerFactory.getLogger(PriceSaleOlTpRest.class);

	@Autowired
	private PriceSaleOlTpService priceSaleOlTpService;

	@ResponseBody
	@RequestMapping(value = "/price/querySalePrice/{startAreaId}/{endAreaId}", method = RequestMethod.GET)
	@ApiOperation(value = "查询产品标准售价", notes = "查询产品标准售价", produces = MediaType.APPLICATION_JSON_VALUE)
	public ResultDTOWithPagination<List<PriceSaleOlTpDTO>> queryPrice(@ApiParam(value = "起点区域ID", required = true) @PathVariable("startAreaId") Integer startAreaId,
																	  @ApiParam(value = "终点区域ID", required = true) @PathVariable("endAreaId") Integer endAreaId
	) throws Exception {
		LOGGER.info("PriceSaleOlTpRest.queryPrice param: {}", startAreaId, endAreaId);
		ResultDTOWithPagination<List<PriceSaleOlTpDTO>> result = new ResultDTOWithPagination<List<PriceSaleOlTpDTO>>(true, null, "查询成功！");
		try {
			List<LinePriceSaleOlTpTool> list = priceSaleOlTpService.selectPrice(null, startAreaId, endAreaId);
			//   PriceSaleOlTpDTO[] listDTO = new PriceSaleOlTpDTO[list.size()];
			List<PriceSaleOlTpDTO> relist = new ArrayList<PriceSaleOlTpDTO>();
			for (int i = 0; i < list.size(); i++) {
				PriceSaleOlTpDTO dto = new PriceSaleOlTpDTO();
				BeanUtils.copyProperties(list.get(i), dto);
				//listDTO[i] = dto;
				relist.add(dto);
			}
			result.setData(relist);
		} catch (Exception e) {
			LOGGER.error("PriceSaleOlTpRest.queryPrice error: {}", e);
			throw new BusinessException(e.getMessage());
		}
		return result;
	}
}
